<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Mail
 *
 * @author wmocarski
 */
class My_Mail extends Zend_Mail {
	
	/**
	 * lista skryptów do renderowania maili
	 */
	private $_renderScripts = array(
		'invoice-edit' => 'invoice-edit.phtml',
		'invoice-view' => 'invoice-view.phtml',
	);

    /**
     * Mail character set
     * @var string
     */
    protected $_charset = 'UTF-8';
    protected $_script;
    protected $_data;
    protected $_subdomain;
    
    public function setScript($script) {
    	$this->_script = $script;
    }
    
    public function setData($data) {
    	$this->_data = $data;
    }
    
    public function setSubdomain($subdomain) {
    	$this->_subdomain = $subdomain;
    }
    
    public function setSubject($subject, $args = false) {
    	$translate = Zend_Registry::get('Zend_Translate');
    	if ($args != false)
    		$subject = vsprintf($translate->translate($subject), $args);
    	else
    		$subject = $translate->translate($subject);
    	
    	if (APPLICATION_ENV != 'production') {
        	$subject = '[' . APPLICATION_ENV . '] ' . $subject;
        	parent::setSubject($subject);
        }
        else
        	parent::setSubject($subject);
    }
    
    public function setFrom($email, $name = null) {
    	parent::setFrom($email, $name);
    }
    
    public function sendEmail() {     
    	$config = Zend_Registry::get('config');
        $mailSettings = Zend_Registry::get('Zend_Mail');
        $transport = new Zend_Mail_Transport_Smtp($mailSettings['host'], $mailSettings['transport']);
        
//         $transport = new My_Mail_Transport_File();
//         $transport->setSavePath(APPLICATION_PATH . '/data/mails');
        
        // renderowanie widoku
        $view = new Zend_View();
        $view->setBasePath(APPLICATION_PATH . '/emails');
        $view->addScriptPath(APPLICATION_PATH . '/layouts/mails/');
        $view->addScriptPath(APPLICATION_PATH . '/views/mails/');
        $view->addScriptPath(APPLICATION_PATH . '/modules/');
        
        if ($this->_data)
        	$view->data = $this->_data;
        
        $sdf = $this->_renderScripts[$this->_script];
        
        if ($this->_renderScripts[$this->_script])
        	$content = $view->render($this->_renderScripts[$this->_script]);

        if (!$this->getFrom())
        	$this->setFrom('notification@simpliz.com', 'Simpliz');
//        echo '<html>';
//        echo $content;
//        echo '</html>';
//        die;
        $this->setBodyHtml($content);
        
//         echo $content; die;

        $sent = true;
        try {
            $this->send($transport);
        } catch (Exception $e) {
            $sent = false;
        }

        if ($sent) {
            return true;
        } else {
            return false;
        }        
        
    }
}
